<template> 	
  <div class="page">
	<div class="layui-fluid">
			<div class="layui-row layui-col-space15">
				<div class="layui-col-md12">
					<div class="layui-card">
						
						<div class="layui-card-body">							
							<div class="layui-form" lay-filter="">
								
								<div class="layui-form-item">
									<div class="layui-inline">
									    <label class="layui-form-label">商品名称</label>
									    <div class="layui-input-inline" style="width: 180px;">
									        <input type="text" name="commodityName" placeholder="商品名称(限10字)"  v-model="itemData.commodityName" autocomplete="off" class="layui-input a" maxlength="10">
									    </div>										   
								  </div>
								</div> 														    
						    <div class="layui-form-item">
									<div class="layui-inline">
									    <label class="layui-form-label">拼团人数</label>
									    <div class="layui-input-inline" style="width: 180px;">
									        <input type="number" name="peonumber" placeholder="拼团人数(限10字)"  v-model="itemData.peonumber" autocomplete="off" class="layui-input a" maxlength="10">
									    </div>										   
								  </div>
								</div>
						    <div class="layui-form-item">
									<div class="layui-inline">
									    <label class="layui-form-label">免购数量</label>
									    <div class="layui-input-inline" style="width: 180px;">
									        <input type="number" name="freenumber" placeholder="免购数量(限10字)"  v-model="itemData.freenumber" autocomplete="off" class="layui-input a" maxlength="10">
									    </div>										   
								  </div>
								</div>
						    <div class="layui-form-item">
									<div class="layui-inline">
									    <label class="layui-form-label">拼团时间(小时)</label>
									    <div class="layui-input-inline" style="width: 180px;">
									        <input type="number" name="collageTime" placeholder="拼团时间(限10字)"  v-model="itemData.collageTime" autocomplete="off" class="layui-input a" maxlength="10">
									    </div>										   
								  </div>
								</div>
						    <div class="layui-form-item">
									<div class="layui-inline">
									    <label class="layui-form-label">助力PK时间(小时)</label>
									    <div class="layui-input-inline" style="width: 180px;">
									        <input type="number" name="assistingTime" placeholder="助力PK时间"  v-model="itemData.assistingTime" autocomplete="off" class="layui-input a" maxlength="10">
									    </div>										   
								  </div>
								</div>
						    <div class="layui-form-item">					        
				        	<label class="layui-form-label">状态</label>
				        	<div class="layui-input-block" style="width:180px">
										<select name="status" lay-filter="status"  v-model="itemData.status">
											<option value="1">启用</option>
											<option value="2">禁用</option>
										</select>
									</div>										
				        </div>

								<div class="layui-form-item">
									<div class="layui-input-block">
										<button class="layui-btn layui-btn-normal" lay-submit lay-filter="setmyinfo" @click="confirmOk">确认</button>
									</div>
								</div>
							</div>
						</div>
					</div>
				</div>
			</div>
		</div>		
		<input type="hidden" id='id'/>
  </div> 
</template>
<script>

export default {	
  name: 'shopfree',  
  data () {
    return { 
    	id:'',     	    	    	
    	itemData:[],
    	status:'1',
    	type:'',
    }
  },
  mounted(){ 
  	window.getdates = this.getdates;	
  },
  methods: {  	
		getdates:function(data){
			 var _this = this; 			
			 _this.id=data.id;			      	
	    var url='/freeGoSetting/getFreeGoSettingByShopCommodityId'
	    var dates={
	    	shopCommodityId:data.id,
	    }
			ajaxRequest(url,'GET','json',dates,function(ret,err){
//				alert(JSON.stringify(ret)+"///")
				if(ret.content!=null){
				  _this.itemData=ret.content;
				  _this.type=2;
				  
				}else{					
					_this.itemData=data;
					_this.type=1;				
				}
			})				
			
		},
  	confirmOk:function(){  		
  		var _this = this;   	
			var len = $(".a").length;
			for(var i = 0; i < len; i++) {
				if($(".a").eq(i).val() == ''||$(".a").eq(i).val()<=0) {
					var msg = $(".a").eq(i).attr("placeholder");
					layer.msg('请输入正确的'+msg);
					return;
				}
			}	        
		  if(_this.itemData.peonumber<_this.itemData.freenumber){
        layer.msg("免购数量不能大于拼团人数");
	      return false;
	    }
		  
    	var dates={
    		shopCommodityId:_this.id,
    		commodityName:_this.itemData.commodityName,
    		peonumber:_this.itemData.peonumber,
    		freenumber:_this.itemData.freenumber,
    		collageTime:_this.itemData.collageTime,
    		assistingTime:_this.itemData.assistingTime,
    		status:_this.itemData.status,
    	}
    	if(_this.type==1){
        var url = "/freeGoSetting/createFreeGoSetting";
	    }else{
	      var url = "/freeGoSetting/updateFreeGoSetting";
	      dates.id=_this.itemData.id
	    }  
    	//alert(JSON.stringify(dates))
		  ajaxRequest(url,'GET', 'json',dates, function(ret, err){
				//alert(JSON.stringify(ret))
			  layer.msg("操作成功");
			  setTimeout(function(){
			  	var index = parent.layer.getFrameIndex(window.name);	          	
			    parent.layer.close(index);
		      parent.location.reload(); 
	      },1000)
		  })
  	}
  },
  updated:function(){		
  	var _this = this;  
		layui.use('form', function(){
		  var form = layui.form;
		  form.render();
		   form.on("select(status)", function(data){										
			  _this.itemData.status=data.value;
		  });
		});
		
  },
}
</script>
<style scoped>
</style>
